﻿
function Helper() {
    this.groupBy = function(array, predicate) {
        var grouped = { };
        for (var i = 0; i < array.length; i++) {
            var groupKey = predicate(array[i]);
            if (typeof(grouped[groupKey]) === "undefined")
                grouped[groupKey] = [];
            grouped[groupKey].push(array[i]);
        }

        return grouped;
    };
    this.AddUserDb = function (_userName, func) {
        AysPost("AddUserAd",
            { userName: _userName },
            func);

    };
    this.AddUserDb = function (_userName, func) {
        AysPost("AddUserAd",
            { userName: _userName },
            func);

    };

    this.AddProductCate = function (_prodcate, func) {
        AysPost("AddProductCate",
            { prodcate: _prodcate },
            func);

    };
    this.AddRoleDb = function (_role, func) {
        AysPost("AddRole",
            { role: _role },
            func);

    };

    this.AddProdHier = function (_prodhier, func) {
        AysPost("AddProdHier",
            { prodhier: _prodhier },
            func);

    };
    this.DeleteUserDb = function (_userNames, func) {
        AysPost("DeleteUserAd",
            { userName: _userNames },
            func);

    };

    this.DeleteProdCate = function (_userNames, func) {
        AysPost("DeleteProdCate",
            { userName: _userNames },
            func);

    };
    this.DeleteRoleDb = function (_role, func) {
        AysPost("DeleteRole",
            { roleName: _role },
            func);

    };
    this.DeleteProdHier = function (_prodhier, func) {
        AysPost("DeleteProdHier",
            { prodhier: _prodhier },
            func);

    };
    this.GetUserDb = function (func) {
        AysPost2("GetUserDB",
            {},
            func,
            false);

    };

    this.GetRoleDb = function (func) {
        AysPost("GetRoleDB",
            {},
            func,
            false);

    };

    this.UpdateRoleDb = function (_rolesData, func) {
        AysPost("UpdateRoleDB",
            { rolesData: _rolesData },
            func,
            false);

    };

    this.UpdateUserDb = function (_usersData, func) {
        AysPost("UpdateUserDB",
            { userData: _usersData },
            func,
            false);

    };
    this.UpdateProdGrp = function (_prodgrpData, func) {
        AysPost("UpdateProdGrp",
            { prodgrpData: _prodgrpData },
            func,
            false);

    };
    
    this.GetLastestNDPrice = function (func) {
        AysPost2("GetLastestNDPrice",
            {},
            func,
            false);

    };
    this.GetNDPriceByID = function (_roundID, func) {
        AysPost2("GetLastestNDPrice",
            {ndround: _roundID},
            func,
            false);

    };

    this.InserNDPrice = function (_ndpriceData, func) {
        AysPost("InserNDPrice",
            { ndpriceData: _ndpriceData },
            func);
    };

    this.InserDocument = function (_documentData, func) {
        AysPost("InserDocument",
            { documentData: _documentData },
            func);
    };

    this.UpdateApprovalMapping = function (_mappingtData, func) {
        AysPost("UpdateApprovalMapping",
            { mappingList: _mappingtData },
            func);
    };

    this.UpdateSaleMapping = function (_mappingtData, func) {
        AysPost("UpdateSaleMapping",
            { mappingList: _mappingtData },
            func);
    };

    this.UpdateDocumentStatus = function (_documents, func) {
        AysPost("UpdateDocumentStatus",
            { DocumentList: _documents },
            func);
    };

    this.UpdateListDocumentStatus = function (_documents, func) {
        AysPost("UpdateListDocumentStatus",
            { DocumentList: _documents },
            func);
    };


    this.GetStatusTab = function (func) {
        AysPost2("GetStatusTab",
            {},
            func);
    };

    this.getAllNdPrice = function (func) {
        AysPost2("getAllNdPrice",
            {},
            func);
    };

    this.getLastestNdPriceItemJson = function (func) {
        AysPost2("getLastestNdPriceItemJson",
            {},
            func);
    };
    this.GetAllDocumentByUser = function (_doctype, func) {
        AysPost2("GetDocumentByUser",
            { doctype: _doctype },
            func);
    };
    this.GetAllCompletedDocumentByUser = function (func,_sort) {
        AysPost2("GetDocumentByUser",
            { sort: _sort },
            func);
    };
    this.GetDocumentDetailByDocID = function (_docID, _isdraft, func) {
        AysPost2("GetDocumentDetailByDocID",
            { docID: _docID, isdraft: _isdraft },
            func);
    };

    this.GetColumnDocumentDetailByDocID = function (_docID,_doctype, func) {
        AysPost2("GetColumnDocumentDetailByDocID",
            { docID: _docID, doctype: _doctype },
            func);
    };

    this.GetCustomerBySaleGrp = function (_dc, _doctype, func) {
        AysPost2("GetCustomerBySaleAndDC",
            { dc: _dc, doctype: _doctype },
            func);
    };
    this.GetCustomerBySaleAllDc = function (_doctype, func) {
        AysPost2("GetCustomerBySaleAllDc",
            { doctype: _doctype },
            func);
    };
    
    this.GetProductToSale = function (func) {
        AysPost2("GetProductToSale",
            {},
            func);
    };
    this.GetProductToSaleDocid = function (id,func) {
        AysPost2("GetProductToSale",
            { docid: id },
            func);
    };
    
    this.GetAllSale = function (func) {
        AysPost2("GetAllSale",
            {},
            func);
    };
    this.GetAllSaleCo = function (func) {
        AysPost2("GetAllSaleCo",
            {},
            func);
    };

    this.GetAllApprover = function (func) {
        AysPost2("GetAllApprover",
            {},
            func);
    };
    this.GetDocumentByApproveUser = function (func) {
        AysPost2("GetDocumentByApproveUser",
            {},
            func);
    };
    this.GetDocumentByAdmin = function (func) {
        AysPost2("GetDocumentByAdmin",
            {},
            func);
    };
    this.GetCurrentUserDetail = function (func) {
        AysPost2("GetCurrentUserDetail",
            {},
            func);
    };
    this.GetSaleClientBySaleCo = function (func) {
        AysPost2("GetSaleClientBySaleCo",
            {},
            func);
    };
    this.SetCurrentUserDetail = function (_username, func) {
        AysPost2("SetCurrentUserDetail",
            { username: _username },
            func);
    };
    this.CheckADuser = function (_username, func) {
        AysPost2("CheckADuser",
            { username: _username },
            func);
    };
    this.GetProductCate = function (func, _isall) {
        AysPost2("GetProductCate",
             {isall: _isall},
            func);
    };
    this.GetProductHier = function (func) {
        AysPost2("GetProductHier",
             {},
            func);
    };
    this.GetAllApproverMapping = function (func) {
        AysPost2("GetAllApproverMapping",
             {},
            func);
    };
    this.GetAllSaleCoMapping = function (func) {
        AysPost2("GetAllSaleCoMapping",
             {},
            func);
    };

    this.ExportSaPfile = function (_docIDs, func) {
        AysPost2("ExportSaPfile",
             {docIDs: _docIDs},
            func);
    };

    this.ExportSaPNDfile = function (_docIDs, func) {
        AysPost2("ExportSaPNDfile",
             { ndround: _docIDs },
            func);
    };

    this.DeleteDocs = function (_docIDs, func) {
        AysPost2("DeleteDocs",
             { docIDs: _docIDs },
            func);
    };
    this.ReNewPincode = function (_pinCode, func) {
        AysPost2("ReNewPincode",
             { Pincode: _pinCode },
            func);
    };
    this.VerifyPincode = function (_pinCode, func) {
        AysPost2("VerifyPincode",
             { Pincode: _pinCode },
            func);
    };
    this.GetCustomerListByDocs = function (_docsID, func) {
        AysPost2("GetCustomerListByDocs",
             { DocsID: _docsID },
            func);
    };

    this.GetAllApproveLevel = function (func) {
        AysPost2("GetAllApproveLevel",
             { },
            func);
    };
    this.SaveAllApproveLevel = function (_approveData, func) {
        AysPost2("SaveAllApproveLevel",
            { approveData: _approveData },
            func);
    };
    
}




this.AysPost = function (methodName, postData, func, async) {
    waitingDialog({ title: "Loading...", message: "Please wait." });
    var domain = "/Wcf/Service1.svc/";
    var Async = true;
    //    if (async == false) {
    //        Async = false;
    //    }

    var uriPost = domain + methodName;
    $.ajax({
        type: "POST",
        url: uriPost,
        async: Async,
        data: postData,
        dataType: "json",
        success: function (data) {
            if (func) {
                func(data);
            }
            closeWaitingDialog();

        },
        error: function (d, d2) {
            closeWaitingDialog();
        }
    });
};

this.AysPost2 = function (_methodName, _postData, func, async) {
    waitingDialog({ title: "Loading...", message: "Please wait." });
    var domain = "/page/Handler1.ashx";
    var Async = false;
    //    if (async == false) {
    //        Async = false;
    //    }

    var _data = { metdhodName: _methodName, postData: _postData };
    var uriPost = domain;
    $.ajax({
        type: "POST",
        url: uriPost,
        async: Async,
        data: _data,
        dataType: "json",
        success: function (data) {
            if (func) {
                if (data && data.Error === '-0') {
                    window.location = '/Page/login.aspx';
                }
                func(data);
            }
            //if (_methodName != 'GetCurrentUserDetail') {
            closeWaitingDialog();
            //}
        },
        error: function (d, d2) {
            closeWaitingDialog();
        }
    });
};

String.prototype.format = function () {
    var args = arguments;
    return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
        if (m == "{{") { return "{"; }
        if (m == "}}") { return "}"; }
        return args[n];
    });
};
